﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using CommonBase;
using DBLib;
using SurveillanceLib;

namespace DAL
{
    /// <summary>
    /// 数据操作类，业务逻辑层
    /// </summary>
    public class VWMSDal : DataOperation
    {
        #region 常量

        // WallInformation
        public const string QUERY_GET_ALLWALLDATA_BYWALLINDEX = "select * from WallInformation where WallIndex = {0} order by WallIndex, PanelIndex"; // 查询时
        public const string QUERY_GET_ALLWALLDATA = "select * from WallInformation order by WallIndex, PanelIndex"; // 新增时
        public const string QUERY_DELETE_WALL_BYWALLINDEX = "delete from WallInformation where WallIndex = {0};"; // 删除时

        #endregion

        #region 方法

        #region WallInformation 表操作

        /// <summary>
        /// 新增 WallInformation 项目
        /// </summary>
        /// <param name="wallIndex">大屏序号</param>
        /// <param name="panelIndex">视频窗口序号</param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="channel">所选通道号</param>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <param name="type">设备类型</param>
        /// <returns></returns>
        public int AddWallItem(int wallIndex, int panelIndex,
            string ip, string port, string channel, string userName, string userPwd,
            string type)
        {
            int resultCount = 0;
            DataSet dataSet = new DataSet();

            try
            {
                OleDbDataAdapter oleDbAdapter = base.GetDataAdapter(QUERY_GET_ALLWALLDATA,
                    "db_VWMSConnectionString", string.Format("{0}.exe", Application.ProductName));
                new OleDbCommandBuilder(oleDbAdapter);
                oleDbAdapter.Fill(dataSet, "WallInformation");

                DataRow newRow = dataSet.Tables["WallInformation"].NewRow(); // 添加一个新数据行
                // 添加数据
                newRow["WallIndex"] = wallIndex;
                newRow["PanelIndex"] = panelIndex;
                newRow["IP"] = ip;
                newRow["Port"] = port;
                newRow["Channel"] = channel;
                newRow["UserName"] = userName;
                newRow["UserPwd"] = userPwd;
                newRow["Type"] = type;

                dataSet.Tables["WallInformation"].Rows.Add(newRow);
                resultCount = oleDbAdapter.Update(dataSet, "WallInformation");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
            finally
            {
                dataSet.Dispose();
            }

            return resultCount;
        }

        /// <summary>
        /// 批量新增 WallInformation 项目
        /// </summary>
        /// <param name="wallList"></param>
        /// <returns></returns>
        public int AddWallItem(List<WallInformationModel> wallList)
        {
            if (wallList == null ||
                (wallList != null && wallList.Count <= 0))
            {
                MessageBox.Show("无数据添加！", ConstantString.MSG_COMPANY);
                return 0;
            }

            int resultCount = 0;
            DataSet dataSet = new DataSet();

            try
            {
                OleDbDataAdapter oleDbAdapter = base.GetDataAdapter(QUERY_GET_ALLWALLDATA,
                    "db_VWMSConnectionString", string.Format("{0}.exe", Application.ProductName));
                new OleDbCommandBuilder(oleDbAdapter);
                oleDbAdapter.Fill(dataSet, "WallInformation");

                DataRow newRow;
                foreach (WallInformationModel wall in wallList)
                {
                    newRow = dataSet.Tables["WallInformation"].NewRow(); // 添加一个新数据行
                    // 添加数据
                    newRow["WallIndex"] = wall.WallIndex;
                    newRow["PanelIndex"] = wall.PanelIndex;
                    newRow["IP"] = wall.IP;
                    newRow["Port"] = wall.Port;
                    newRow["Channel"] = wall.Channel;
                    newRow["UserName"] = wall.UserName;
                    newRow["UserPwd"] = wall.UserPwd;
                    newRow["Type"] = wall.Type;

                    dataSet.Tables["WallInformation"].Rows.Add(newRow);
                }

                resultCount = oleDbAdapter.Update(dataSet, "WallInformation");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
            finally
            {
                dataSet.Dispose();
            }

            return resultCount;
        }

        /// <summary>
        /// 根据 wallIndex 删除 WallInformation 项目
        /// </summary>
        /// <param name="wallIndex">大屏序号</param>
        /// <returns></returns>
        public int DeleteWallItem(int wallIndex)
        {
            if (wallIndex < 0)
            {
                MessageBox.Show("大屏序号错误！", ConstantString.MSG_COMPANY);
                return 0;
            }

            try
            {
                string deleteSqlCmd = string.Format(QUERY_DELETE_WALL_BYWALLINDEX, wallIndex);
                return base.RunSqlCommand(deleteSqlCmd, 
                    "db_VWMSConnectionString", string.Format("{0}.exe", Application.ProductName));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
                return 0;
            }
        }

        /// <summary>
        /// 批量根据 wallIndex 删除 WallInformation 项目
        /// </summary>
        /// <param name="wallIndexList">大屏序号组</param>
        /// <returns></returns>
        public int DeleteWallItem(List<int> wallIndexList)
        {
            if (wallIndexList == null ||
                (wallIndexList != null && wallIndexList.Count <= 0))
            {
                MessageBox.Show("无数据删除！", ConstantString.MSG_COMPANY);
                return 0;
            }

            try
            {
                StringBuilder sbDeleteSqlCmd = new StringBuilder();
                string deleteSqlCmd;

                foreach (int wallIndex in wallIndexList)
                {
                    deleteSqlCmd = string.Format(QUERY_DELETE_WALL_BYWALLINDEX, wallIndex);
                    sbDeleteSqlCmd.Append(deleteSqlCmd);
                }

                return base.RunSqlCommand(sbDeleteSqlCmd.ToString(), 
                    "db_VWMSConnectionString", string.Format("{0}.exe", Application.ProductName));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
                return 0;
            }
        }

        #endregion

        #endregion
    }
}
